using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CNodeLabels_OneToOne : CBaseDynamicM2M, IComparable<CNodeLabels_OneToOne>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CNodeLabels_OneToOne(CNodeLabels_OneToOne original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _labelNodeGuid = original.LabelNodeGuid;
            _labelTypeId = original.LabelTypeId;
            _labelTagId = original.LabelTagId;
        }

        //Protected (Datareader/Dataset)
        protected CNodeLabels_OneToOne(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CNodeLabels_OneToOne(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _labelNodeGuid = Guid.Empty;
            _labelTypeId = int.MinValue;
            _labelTagId = int.MinValue;
        }
        #endregion

        #region Members
        protected Guid _labelNodeGuid;
        protected int _labelTypeId;
        protected int _labelTagId;
        #endregion

        #region Properties - Column Values
        //Primary Key Columns
        public Guid LabelNodeGuid    {   get { return _labelNodeGuid; } set { _labelNodeGuid = value; }    }
        public int LabelTypeId    {   get { return _labelTypeId; } set { _labelTypeId = value; }    }

        //Table Columns (Read/Write)
        public int LabelTagId    {   get { return _labelTagId; } set { _labelTagId = value; }    }

        //View Columns (ReadOnly)

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "NodeLabels_OneToOne";
        public const string VIEW_NAME       = "";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "LabelNodeGuid, LabelTypeId";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CNodeLabels_OneToOne other)
        {
            int i = this.LabelNodeGuid.CompareTo(other.LabelNodeGuid) ;
            if (0 != i)
                return i;
            return this.LabelTypeId.CompareTo(other.LabelTypeId) ;
        }

        //Primary Key Information
        protected override string PrimaryKeyName    {   get { return "LabelNodeGuid";    } }
        protected override string SecondaryKeyName  {   get { return "LabelTypeId";  } }        protected override object PrimaryKeyValue 
        {
              get { return _labelNodeGuid; }
              set { _labelNodeGuid = (Guid)value; }
        }
        protected override object SecondaryKeyValue 
        {
              get { return _labelTypeId; }
              set { _labelTypeId = (int)value; }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)        {   return new CNodeLabels_OneToOne(this.DataSrc, row);     }
        protected override CBase MakeFrom(IDataReader dr)     {   return new CNodeLabels_OneToOne(this.DataSrc, dr);      }

        //Factory Methods - List
        protected override IList MakeList()                   {   return new CNodeLabels_OneToOneList();                  }
        protected override IList MakeList(int capacity)       {   return new CNodeLabels_OneToOneList(capacity);          }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _labelNodeGuid = CAdoData.GetGuid(dr, "LabelNodeGuid");
            _labelTypeId = CAdoData.GetInt(dr, "LabelTypeId");
            _labelTagId = CAdoData.GetInt(dr, "LabelTagId");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _labelNodeGuid = CAdoData.GetGuid(dr, "LabelNodeGuid");
            _labelTypeId = CAdoData.GetInt(dr, "LabelTypeId");
            _labelTagId = CAdoData.GetInt(dr, "LabelTagId");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("LabelNodeGuid", CAdoData.NullVal(_labelNodeGuid));
            data.Add("LabelTypeId", CAdoData.NullVal(_labelTypeId));
            data.Add("LabelTagId", CAdoData.NullVal(_labelTagId));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Normally used to load the cache
        public    new CNodeLabels_OneToOneList SelectAll()                                                                           {   return (CNodeLabels_OneToOneList)base.SelectAll();        }
        public    new CNodeLabels_OneToOneList SelectAll(string orderBy)                                                             {   return (CNodeLabels_OneToOneList)base.SelectAll(orderBy); }

        //Sometimes use a custom query to load the cache
        protected new CNodeLabels_OneToOneList SelectWhere(string colName, ESign sign, object colValue)                              {   return (CNodeLabels_OneToOneList)base.SelectWhere(colName, sign, colValue);                  }
        protected new CNodeLabels_OneToOneList SelectWhere(CCriteria where)                                                          {   return (CNodeLabels_OneToOneList)base.SelectWhere(where);                                    }
        protected new CNodeLabels_OneToOneList SelectWhere(CCriteriaList where)                                                      {   return (CNodeLabels_OneToOneList)base.SelectWhere(where);                                    }
        protected new CNodeLabels_OneToOneList SelectWhere(CCriteriaList where, string tableOrJoin)                                  {   return (CNodeLabels_OneToOneList)base.SelectWhere(where, tableOrJoin, this.OrderByColumns);  }
        protected new CNodeLabels_OneToOneList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy)                  {   return (CNodeLabels_OneToOneList)base.SelectWhere(where, tableOrJoin, orderBy);              }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces (or a stored proc)")]
        protected new CNodeLabels_OneToOneList SelectWhere(string unsafeWhereClause)                                                 {   return (CNodeLabels_OneToOneList)base.SelectWhere(unsafeWhereClause);                        }
        protected     CNodeLabels_OneToOneList SelectById(Guid labelNodeGuid, int labelTypeId)                                               {   return (CNodeLabels_OneToOneList)base.SelectById(labelNodeGuid, labelTypeId);                    }
        protected     CNodeLabels_OneToOneList SelectByIds(List<Guid> ids)                                         {   return (CNodeLabels_OneToOneList)base.SelectByIds(ids);                                      }

        //Select Queries - Paged
        protected new CNodeLabels_OneToOneList SelectAll(  CPagingInfo pi)                                               {    return (CNodeLabels_OneToOneList)base.SelectAll(  pi);                          }
        protected new CNodeLabels_OneToOneList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)        {    return (CNodeLabels_OneToOneList)base.SelectWhere(pi, name, sign, value);       }
        protected new CNodeLabels_OneToOneList SelectWhere(CPagingInfo pi, CCriteria criteria)                           {    return (CNodeLabels_OneToOneList)base.SelectWhere(pi, criteria);                }
        protected new CNodeLabels_OneToOneList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                       {    return (CNodeLabels_OneToOneList)base.SelectWhere(pi, criteria);                }
        protected new CNodeLabels_OneToOneList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)    {    return (CNodeLabels_OneToOneList)base.SelectWhere(pi, criteria, viewOrJoin);    }

        //Select Queries - Transactional (Internal scope for use in cascade deletes)
        internal new CNodeLabels_OneToOneList SelectAll(                                                                                           IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectAll(                                                 tx);    }
        internal new CNodeLabels_OneToOneList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectAll(orderBy,                                         tx);    }
        internal new CNodeLabels_OneToOneList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectWhere(columnName,              columnValue,          tx);    }
        internal new CNodeLabels_OneToOneList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        internal new CNodeLabels_OneToOneList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectWhere(               criteria,                       tx);    }
        internal new CNodeLabels_OneToOneList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectWhere(               criteria,                       tx);    }
        internal new CNodeLabels_OneToOneList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        internal new CNodeLabels_OneToOneList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        internal     CNodeLabels_OneToOneList SelectById(Guid labelNodeGuid, int labelTypeId,                                                              IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectById(labelNodeGuid, labelTypeId,                         tx);    }
        internal     CNodeLabels_OneToOneList SelectByIds(List<Guid> ids,                                                        IDbTransaction tx)  {    return (CNodeLabels_OneToOneList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        protected new CNodeLabels_OneToOneList MakeList(string storedProcName,                           IDbTransaction txOrNull)    {   return (CNodeLabels_OneToOneList)base.MakeList(storedProcName,             txOrNull);  }
        protected new CNodeLabels_OneToOneList MakeList(string storedProcName, object[] parameters,      IDbTransaction txOrNull)    {   return (CNodeLabels_OneToOneList)base.MakeList(storedProcName, parameters, txOrNull);  }
        protected new CNodeLabels_OneToOneList MakeList(string storedProcName, CNameValueList parameters,IDbTransaction txOrNull)    {   return (CNodeLabels_OneToOneList)base.MakeList(storedProcName, parameters, txOrNull);  }
        protected new CNodeLabels_OneToOneList MakeList(string storedProcName, List<object> parameters,  IDbTransaction txOrNull)    {   return (CNodeLabels_OneToOneList)base.MakeList(storedProcName, parameters, txOrNull);  }
        protected new CNodeLabels_OneToOneList MakeList(string storedProcName, int param1,               IDbTransaction txOrNull)    {   return (CNodeLabels_OneToOneList)base.MakeList(storedProcName, param1,     txOrNull);  }
        protected new CNodeLabels_OneToOneList MakeList(string storedProcName, string param1,            IDbTransaction txOrNull)    {   return (CNodeLabels_OneToOneList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CNodeLabels_OneToOneList MakeList(DataSet              ds) { return (CNodeLabels_OneToOneList)base.MakeList(ds);        }
        protected new CNodeLabels_OneToOneList MakeList(DataTable            dt) { return (CNodeLabels_OneToOneList)base.MakeList(dt);        }
        protected new CNodeLabels_OneToOneList MakeList(DataRowCollection  rows) { return (CNodeLabels_OneToOneList)base.MakeList(rows);      }
        protected new CNodeLabels_OneToOneList MakeList(IDataReader          dr) { return (CNodeLabels_OneToOneList)base.MakeList(dr);        }
        protected new CNodeLabels_OneToOneList MakeList(object           drOrDs) { return (CNodeLabels_OneToOneList)base.MakeList(drOrDs);    }
        protected new CNodeLabels_OneToOneList MakeList(byte[]             gzip) { return (CNodeLabels_OneToOneList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Note: These methods should only be used for cascade-deletes, or initialising the cache. Use CNodeLabels_OneToOne.Cache.GetBy... for reqular queries

        //Non-Paged
        protected CNodeLabels_OneToOneList SelectByTagId(int labelTagId)    {    return SelectWhere(new CCriteriaList("LabelTagId", labelTagId));    }
        protected CNodeLabels_OneToOneList SelectByNodeGuid(Guid labelNodeGuid)    {    return SelectWhere(new CCriteriaList("LabelNodeGuid", labelNodeGuid));    }
        protected CNodeLabels_OneToOneList SelectByTypeId(int labelTypeId)    {    return SelectWhere(new CCriteriaList("LabelTypeId", labelTypeId));    }

        //Paged
        protected CNodeLabels_OneToOneList SelectByTagId(CPagingInfo pi, int labelTagId)    {    return SelectWhere(pi, new CCriteriaList("LabelTagId", labelTagId));    }
        protected CNodeLabels_OneToOneList SelectByNodeGuid(CPagingInfo pi, Guid labelNodeGuid)    {    return SelectWhere(pi, new CCriteriaList("LabelNodeGuid", labelNodeGuid));    }
        protected CNodeLabels_OneToOneList SelectByTypeId(CPagingInfo pi, int labelTypeId)    {    return SelectWhere(pi, new CCriteriaList("LabelTypeId", labelTypeId));    }

        //Count
        protected int SelectCountByTagId(int labelTagId)   {   return SelectCount(new CCriteriaList("LabelTagId", labelTagId));     }
        protected int SelectCountByNodeGuid(Guid labelNodeGuid)   {   return SelectCount(new CCriteriaList("LabelNodeGuid", labelNodeGuid));     }
        protected int SelectCountByTypeId(int labelTypeId)   {   return SelectCount(new CCriteriaList("LabelTypeId", labelTypeId));     }

        //Transactional
        internal CNodeLabels_OneToOneList SelectByTagId(int labelTagId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("LabelTagId", labelTagId), tx);    }
        internal CNodeLabels_OneToOneList SelectByNodeGuid(Guid labelNodeGuid, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("LabelNodeGuid", labelNodeGuid), tx);    }
        internal CNodeLabels_OneToOneList SelectByTypeId(int labelTypeId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("LabelTypeId", labelTypeId), tx);    }
        #endregion

        #region Many-To-Many Helper Functions
        //Insert/Delete - NonTransactional
        public static void InsertPair(Guid labelNodeGuid, int labelTypeId) {        InsertPair(labelNodeGuid, labelTypeId, null); }
        public static int  DeletePair(Guid labelNodeGuid, int labelTypeId) { return DeletePair(labelNodeGuid, labelTypeId, null); }
        public static bool ExistsPair(Guid labelNodeGuid, int labelTypeId) { return ExistsPair(labelNodeGuid, labelTypeId, null); }
        
        //Insert/Delete - Transactional
        public static void InsertPair(Guid labelNodeGuid, int labelTypeId, IDbTransaction txOrNull) {        InsertPair(labelNodeGuid, labelTypeId, txOrNull, CDataSrc.Default); }
        public static int  DeletePair(Guid labelNodeGuid, int labelTypeId, IDbTransaction txOrNull) { return DeletePair(labelNodeGuid, labelTypeId, txOrNull, CDataSrc.Default); }
        public static bool ExistsPair(Guid labelNodeGuid, int labelTypeId, IDbTransaction txOrNull) { return ExistsPair(labelNodeGuid, labelTypeId, txOrNull, CDataSrc.Default); }

        //Insert/Delete
        public static void InsertPair(Guid labelNodeGuid, int labelTypeId, IDbTransaction txOrNull, CDataSrc dataSrc)
        {
            CNodeLabels_OneToOne pair = new CNodeLabels_OneToOne(dataSrc);
            pair.LabelNodeGuid = labelNodeGuid;
            pair.LabelTypeId = labelTypeId;
            pair.Save(txOrNull);
        }
        public static int DeletePair(Guid labelNodeGuid, int labelTypeId, IDbTransaction txOrNull, CDataSrc dataSrc)
        {
            CCriteriaList where = new CCriteriaList();
            where.Add("LabelNodeGuid", labelNodeGuid);
            where.Add("LabelTypeId", labelTypeId);

            //Faster, but not audited
            //return new CNodeLabels_OneToOne(dataSrc).DeleteWhere(where, txOrNull, true);
            
            //Slower, but audit-trail and other delete logic is preserved e.g. cascade deletes
            CNodeLabels_OneToOneList list = new CNodeLabels_OneToOne(dataSrc).SelectWhere(where, txOrNull);
            list.DeleteAll(txOrNull);
            return list.Count;
        }
        public static bool ExistsPair(Guid labelNodeGuid, int labelTypeId, IDbTransaction txOrNull, CDataSrc dataSrc)
        {
            return new CNodeLabels_OneToOne(dataSrc).SelectById(labelNodeGuid, labelTypeId, txOrNull).Count > 0;
        }
        #endregion

        #region Static - Cache Implementation
        public static CNodeLabels_OneToOneList Cache
        {
            get
            {
                CNodeLabels_OneToOneList cache = (CNodeLabels_OneToOneList)CCache.Get(CACHE_KEY);
                if (cache == null)
                {
                    lock (typeof(CNodeLabels_OneToOneList))
                    {
                        cache = (CNodeLabels_OneToOneList)CCache.Get(CACHE_KEY);
                        if (cache == null)
                        {
                            cache = LoadCache();
                            CNodeLabels_OneToOne.Cache = cache;
                        }
                    }
                }
                return cache;
            }
            set
            {
                SetCache(value);   //Not locked, because cache gets cleared at anytime anyway
            }
        }

        //Change Management:
        //Clone, modify, and then replace the cache (threadsafe for anything iterating the collection)
        //Note that internal indices are dicarded each time unless handled in constructor and add/remove overrides
        protected override void CacheDelete()
        {
            if (CacheIsNull)
                return;

            lock (typeof(CNodeLabels_OneToOneList))
            {
                CNodeLabels_OneToOneList temp = new CNodeLabels_OneToOneList(Cache);
                int size = temp.Count;
                temp.Remove(this);
                if (size == temp.Count) //Remove might fail if cache was refreshed with new instances. Use old index
                    temp.Remove(CacheGetById(temp));
                Cache = temp;
            }
        }
        protected override void CacheInsert()
        {
            if (CacheIsNull)
                return;

            lock (typeof(CNodeLabels_OneToOneList))
            {
                CNodeLabels_OneToOneList temp = new CNodeLabels_OneToOneList(Cache);
                temp.Add(this);
                SetCache(temp);
            }
        }
        protected override void CacheUpdate()
        {
            if (CacheIsNull)
                return;

            lock (typeof(CNodeLabels_OneToOneList))
            {
                CNodeLabels_OneToOneList temp = new CNodeLabels_OneToOneList(Cache);
                if (!temp.Contains(this))
                {
                    temp.Remove(CacheGetById(temp));
                    temp.Add(this);
                }
                SetCache(temp);
            }
        }
        protected override void CacheClear()
        {
            SetCache(null);
        }
        protected static bool CacheIsNull
        {
            get
            {
                return null == CCache.Get(CACHE_KEY);
            }
        }
        #endregion
        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "LabelNodeGuid", this.LabelNodeGuid);
            Store(w, "LabelTypeId", this.LabelTypeId);
            Store(w, "LabelTagId", this.LabelTagId);
        }
        #endregion

    }
}
